package com.clip.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzServletContextListener implements ServletContextListener {
	public static final String QUARTZ_FACTORY_KEY ="org.quartz.impl.StdSchedulerFactory.KEY";
 
 
	private StdSchedulerFactory factory = null;   

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		 try {   
             factory.getDefaultScheduler().shutdown();   
        } catch (SchedulerException ex) {   
             ex.printStackTrace(); 
        }   
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext context=sce.getServletContext();
		try {
			factory = new StdSchedulerFactory();   
			factory.getScheduler().start();
			context.setAttribute(QUARTZ_FACTORY_KEY, factory);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
